const ImgParam = require("../models/ImgParam");


module.exports = class ImgParamManager {
  /**
   * 增加一个ImgParam
   * @param {*} ImgParam
   * @returns
   */
  static async addImgParam(paramObj) {
    const _this = this;
    const find_res = await _this.getImgParamById(paramObj.id)
    console.log('++++++++++++++++++++addImgParam:find_res',find_res)
    if(find_res){
        const new_params = {...paramObj};
        const id = new_params.id;
        delete new_params.id;
        const result = await _this.updateImgParam(id,new_params)
        return result.toJSON();
    }else{
        const result = await ImgParam.create(paramObj);
        return result.toJSON();
    }
  }

  /**
   * 删除一个ImgParam
   * @param {*} paramId
   */
  static async deleteImgParam(paramId) {
    const result = await ImgParam.destroy({
      where: {
        id: paramId,
      },
    });
  }
  /**
   * 批量删除ImgParam
   * @param {*} id
   */
  static async bulkDeleteImgParams(params) {
    let promises;
    const _this = this;
    const delRes = [];
    if (params.length > 0) {
      promises = params.map(async (item) => {
        const res = await _this.deleteImgParam(item);
        delRes.push(res);
      });
    }
    await Promise.all(promises);
  }

  /**
   * 更新user
   * @param {*} paramId
   * @param {*} paramObj
   */
  static async updateImgParam(paramId, paramObj) {
    const updateData = {
       params:JSON.stringify(paramObj)
    }
    await ImgParam.update(updateData, {
      where: {
        id: paramId,
      },
    });
    const result = await ImgParam.findOne({
      where: {
        id: paramId,
      },
    });
    return result;
  }

  /**
   * 查询所有的user
   * @returns
   */
  static async getAllParams() {
    const result = await ImgParam.findAll();
    return JSON.parse(JSON.stringify(result));
  }

  /**
   * 根据id查询ImgParam
   * @returns
   */
  static async getImgParamById(id) {
    const result = await ImgParam.findOne({
      where: {
        id,
      },
    });
    if (result === null) {
      console.log("getAdminById user Not found!");
    } else {
      return JSON.parse(JSON.stringify(result));
    }
  }
}